package cn.bear.spring_boot_study.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 增强版MyBatis-Plus代码生成器
 * 支持交互式配置
 * 
 * @author bear
 * @since 2024-01-01
 */
public class EnhancedCodeGenerator {

    public static void main(String[] args) {
        // 交互式配置
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("=== MyBatis-Plus 代码生成器 ===");
        System.out.print("请输入模块名（默认：system）：");
        String moduleName = scanner.nextLine();
        if (moduleName.trim().isEmpty()) {
            moduleName = "system";
        }
        
        System.out.print("请输入表名（多个表用逗号分隔，默认：user,role,permission）：");
        String tableNames = scanner.nextLine();
        if (tableNames.trim().isEmpty()) {
            tableNames = "user,role,permission";
        }
        
        System.out.print("请输入表前缀（将被移除，默认：sys_）：");
        String tablePrefix = scanner.nextLine();
        if (tablePrefix.trim().isEmpty()) {
            tablePrefix = "sys_";
        }
        
        System.out.print("请输入作者名（默认：bear）：");
        String author = scanner.nextLine();
        if (author.trim().isEmpty()) {
            author = "bear";
        }
        
        // 使用新的FastAutoGenerator API生成代码
        final String projectPath = System.getProperty("user.dir");
        final String[] tableArray = tableNames.split(",");
        final String finalModuleName = moduleName;
        final String finalAuthor = author;
        final String finalTablePrefix = tablePrefix;
        
        FastAutoGenerator.create(GeneratorConfig.DB_URL, GeneratorConfig.DB_USERNAME, GeneratorConfig.DB_PASSWORD)
                .globalConfig(builder -> {
                     builder.author(finalAuthor)
                             .outputDir(projectPath + "/src/main/java")
                             .disableOpenDir()
                             .dateType(DateType.ONLY_DATE);
                 })
 
                 .packageConfig(builder -> {
                     builder.parent(GeneratorConfig.PARENT_PACKAGE)
                             .moduleName(finalModuleName)
                             .entity("entity")
                             .mapper("mapper")
                             .service("service")
                             .serviceImpl("service.impl")
                             .controller("controller")
                             .pathInfo(java.util.Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper/" + finalModuleName + "/"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableArray)
                            .addTablePrefix(finalTablePrefix)
                            .entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .addTableFills(
                                    new Column("create_time", FieldFill.INSERT),
                                    new Column("update_time", FieldFill.INSERT_UPDATE),
                                    new Column("create_by", FieldFill.INSERT),
                                    new Column("update_by", FieldFill.INSERT_UPDATE)
                            )
                            .logicDeleteColumnName("deleted")
                            .idType(IdType.ASSIGN_ID)
                            .controllerBuilder()
                            .enableRestStyle()
                            .enableHyphenStyle()
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .mapperBuilder()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
        
        System.out.println("代码生成完成！");
        scanner.close();
    }
    

}